package com.fenshi.presenter.imp;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;

import com.alibaba.fastjson.JSONObject;
import com.android.volley.VolleyError;
import com.fenshi.bean.CarBean;
import com.fenshi.interactor.SendOrderInteractor;
import com.fenshi.interactor.imp.SendOrderInteractorImp;
import com.fenshi.presenter.SendOrderPresenter;
import com.fenshi.presenter.listener.RequestListener;
import com.fenshi.until.CookicUntil;
import com.fenshi.until.SystemUtils;
import com.fenshi.view.SendOrderView;
import com.quickdv.activity.BaseActivity;

/**
 * Created by lilongfei on 15/9/21.
 */
public class SendOrderPresenterImp implements SendOrderPresenter,RequestListener<JSONObject> {

    SendOrderInteractor interactor;
    SendOrderView view;
    Context c;
    CarBean car;

    int timeType = -1;

    public SendOrderPresenterImp(Context c){
        this(c,(SendOrderView)c);
    }

    public SendOrderPresenterImp(Context c, SendOrderView view){
        this.c =c;
        this.view =view;
        interactor = new SendOrderInteractorImp(c,this);

        Bundle bundle = ((Activity)c).getIntent().getExtras();
        if(bundle !=null) car = (CarBean) bundle.getSerializable("car");
    }

    @Override
    public boolean checkText(String text) {
        if (text.length()>45) {
            view.setTextError("长度不可超过50字符");
            return false;
        }
        return true;
    }

    @Override
    public void submit(int sendType, String startTime, String endTime,String description) {
        String userId = CookicUntil.getUser().getUserId();
        String cardId = ((Activity) c).getIntent().getExtras()!=null ?((Activity) c).getIntent().getExtras().getString("cardId"):null;
        if (userId == null){
            view.showError("用户未登陆");
            return;
        }

        if (sendType == -1){
            view.showError("没有设置租用类型");
            return;
        }
//        if (sendType != 1 && (startTime == null || startTime.length() ==0 || endTime == null || endTime.length()==0)){
//            view.showError("租用时间不可为空");
//            return;
//        }
        if (car ==null) return;
        if (cardId ==null || cardId.length() ==0) return;

        String currentTime = SystemUtils.getCurrentTime();
        interactor.sendOrder(userId,cardId,car.getCarId(),car.getCarType(),0,sendType,currentTime,startTime,endTime);
        view.showAlert();
    }

    @Override
    public void checkTime(int type) {
        this.timeType = type;
    }

    @Override
    public void bindTime(String time) {
        if (timeType == 0){
            view.setStartTime(time);
        }else if (timeType == 1){
            view.setEndTime(time);
        }
    }

    @Override
    public void Success(String tag, JSONObject result) {
        view.dismissAlert();
        if (result!=null){
            String orderId = result.getString("orderId");
        }
        view.showError("车辆已解锁成功,欢迎使用");

        if (Build.VERSION.SDK_INT >15)
            ((BaseActivity)c).finishAffinity();
        else
            ((BaseActivity)c).finish();
    }

    @Override
    public void Error(String tag, String error) {
        view.dismissAlert();
        view.showError(error);
    }

    @Override
    public void Exception(String tag, VolleyError error) {
        view.dismissAlert();
        view.showError("服务器异常，请稍后重试");
    }

    @Override
    public void initViewAndEvent() {
        CarBean carBean = ((CarBean) ((Activity) c).getIntent().getExtras().getSerializable("car"));
        view.initViewOrEvent(carBean);
    }
}
